package com.itheima.service.Impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Login;
import com.itheima.pojo.Users;
import com.itheima.service.UserService;
import com.itheima.utial.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public void register(Users user) {
        userMapper.register(user);
    }

    //登录
    @Override
    public Login login(Users user) {
        Users user1=userMapper.login(user);
        if(user1!= null){
            Map<String,Object> dataMap=new HashMap<>();
            dataMap.put("id",user1.getId());
            dataMap.put("email",user1.getEmail());

            String token = JwtUtils.generateJwt(dataMap);
            return new Login(user1.getId(),user1.getName(),user1.getEmail(),token);

        }else{
            return  null;
        }

    }
}
